<template>
    <div class="login-box">
        <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="100px" hide-required-asterisk>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="loginForm.password" type="password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="LoginCreate">立即创建</el-button>
                <el-button @click="resetLoginForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                // 表单验证规则
                loginFormRules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ]
                }
            }
        },
        methods: {
            // 重置表单
            resetLoginForm() {
                this.$refs.loginFormRef.resetFields()
            },

            // 提交表单数据
            async LoginCreate() {
                const {data: res} = await this.$http.post('api/permission/getMenu', this.loginForm)
                console.log(res.data.menu)
                if(res.code === 20000) {
                    // 登录成功后，先清空菜单，避免菜单重复或本地有缓存
                    this.$store.commit('clearMenu')

                    // 将返回的菜单数据，提交到vuex中
                    this.$store.commit('setMenu', res.data.menu)

                    // 动态添加路由
                    this.$store.commit('addMenu', this.$router)

                    this.$router.push({name: 'home'})
                }else {

                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .login-box {
        border: solid 1px #999999;
        width: 400px;
        height: 300px;
        display: flex;
        align-items: center;
    }
</style>